﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.ServiceProcess;
using System.Timers;
using System.Xml;

//servisi installutil /i TCMB.exe ile kur (installutil /u TCMB.exe ile kaldırılıyor) ve sonra services den başlat!
/*
CD C:\Program Files\Karbel\ConfExpert TCMB\
c:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil /u tcmb.exe
\Windows\Microsoft.NET\Framework\v4.0.30319\installutil /i tcmb.exe
*/
namespace TCMB
{
    public partial class TimerSrv : ServiceBase
    {
        public TimerSrv()
        {
            InitializeComponent();
        }

        System.Timers.Timer timer = new System.Timers.Timer();
        protected override void OnStart(string[] args)
        {
            AddLog("starting service", EventLogEntryType.Information);
            timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
            timer.Enabled = true;
        }

        protected override void OnStop()
        {
            timer.Enabled = false;
            AddLog("stopping service", EventLogEntryType.Information);
        }

        private void OnElapsedTime(object source, ElapsedEventArgs e)
        {
            try
            {
                timer.Enabled = false;

                XmlTextReader rdr = new XmlTextReader("http://www.tcmb.gov.tr/kurlar/today.xml");
                XmlDocument myxml = new XmlDocument();
                myxml.Load(rdr);

                Save(myxml.InnerXml);
                DateTime NextCheck = DateTime.Today.AddDays(1).AddHours(17).AddMinutes(0);
                timer.Interval = NextCheck.Subtract(DateTime.Now).TotalMilliseconds;
            }
            catch (Exception Ex)
            {
                AddLog(Ex.ToString(), EventLogEntryType.Error);
                timer.Interval = 6e6; //10 minutes! 600 sn.
            }
            finally
            {
                timer.Enabled = true;
                AddLog("Next check " + timer.Interval.ToString() + " ms. later", EventLogEntryType.Information);
            }

        }

        public void Save(string xmlstr)
        {
            using (SqlConnection sqlConnection = new SqlConnection(@"Data Source=karbelerp.karbel.com,1430;Initial Catalog=ConfExpert;Persist Security Info=True;Integrated Security=False;User ID=CEUser;Password=1034!1518"))
            {
                try
                {
                    sqlConnection.Open();
                    SqlCommand sel = new SqlCommand("SaveCurrency", sqlConnection);
                    sel.CommandType = System.Data.CommandType.StoredProcedure;
                    sel.Parameters.Add("@xmlstr", SqlDbType.VarChar);
                    sel.Parameters[0].Value = xmlstr;
                    sel.ExecuteScalar();
                }
                catch (Exception Ex)
                {
                    throw Ex;
                }
                finally
                {
                    AddLog("updated.", EventLogEntryType.Information);
                    sqlConnection.Close();
                }
            }
        }


        private void AddLog(string message, EventLogEntryType eventLogEntryType)
        {
            string Source = "ConfExpert TCMB", LogName = "TCMB Service";

            if (!EventLog.SourceExists(Source))
                EventLog.CreateEventSource(Source, LogName);

            EventLog aLog = new EventLog();
            aLog.Source = Source;
            aLog.WriteEntry(message, eventLogEntryType);
        }

    }
}


